package com.xci.sys.controller;

import com.xci.core.annotation.Authorize;
import com.xci.core.base.RestMessage;
import com.xci.core.base.WebController;
import com.xci.core.exceptions.EntityNotFoundException;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.helper.Helper;
import com.xci.core.helper.TreeHelper;
import com.xci.core.internal.Const;
import com.xci.core.web.TreeNode;
import com.xci.core.web.TreeNodeIndex;
import com.xci.sys.entity.SysDicCategory;
import com.xci.sys.service.DicCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统字典类型控制器
 * @author 吕艳阳
 */
@Authorize
@Slf4j
@Controller
@RequestMapping("/sys/dicCategory")
public class DicCategoryController extends WebController {
    /**
     * 字典类型服务
     */
    @Resource
    private DicCategoryService dicCategoryService;

    //region 页面视图

    /**
     * 字典类型首页
     */
    @GetMapping
    @Authorize
    public String index() {
        return "sys/dic/index";
    }

    /**
     * 字典类型新建页面
     */
    @GetMapping("/create")
    @Authorize
    public String create(String parentId, ModelMap map) {
        createMark(map);
        SysDicCategory entity = new SysDicCategory();
        entity.setId(Helper.nextIdStr());
        entity.setParentId(parentId);
        map.put("entity", entity);
        map.put("parentName", getParentName(parentId));
        return "sys/dic/edit";
    }

    /**
     * 字典类型编辑页面
     */
    @GetMapping("/edit")
    @Authorize
    public String edit(String id, ModelMap map) {
        SysDicCategory entity = dicCategoryService.selectById(id);
        if (entity == null) throw new EntityNotFoundException(id);
        map.put("entity", entity);
        map.put("parentName", getParentName(entity.getParentId()));
        return "sys/dic/edit";
    }

    private String getParentName(String id) {
        String parentName = Const.EMPTY;
        if (Helper.isNotBlank(id) && !id.equals(Const.ROOT_NODE_ID)) {
            var parentModel = dicCategoryService.selectById(id);
            if (parentModel!= null){
                parentName = parentModel.getName();
            }
        }
        if (Helper.isBlank(parentName)){
            parentName = "顶层字典类型";
        }
        return parentName;
    }

    /**
     * 字典类型详细页面
     */
    @GetMapping("/details")
    @Authorize
    public String details(String id, ModelMap map) {
        SysDicCategory entity = dicCategoryService.selectById(id);
        if (entity == null) throw new EntityNotFoundException(id);
        map.put("entity", entity);
        return "sys/dic/details";
    }

    //endregion

    //region 数据处理

    /**
     * 字典类型Tree
     */
    @ResponseBody
    @GetMapping("/tree")
    public Object tree() {
        List<SysDicCategory> list = dicCategoryService.selectList();
        List<TreeNode> models = dicCategoryService.convertToNodeList(list);
        return RestMessage.tree(models);
    }

    /**
     * 字典类型表格数据
     */
    @ResponseBody
    @GetMapping("/grid")
    @Authorize
    public Object grid() {
        List<SysDicCategory> list = dicCategoryService.selectList();
        return RestMessage.grid(list);
    }

    /**
     * 保存新增字典类型
     */
    @ResponseBody
    @PostMapping("/create-save")
    @Authorize
    public RestMessage createSave(@ModelAttribute SysDicCategory entity ) {
        return dicCategoryService.insert(entity);
    }

    /**
     * 保存修改字典类型
     */
    @ResponseBody
    @PostMapping("/edit-save")
    @Authorize
    public RestMessage editSave(@ModelAttribute SysDicCategory entity ) {
        return dicCategoryService.update(entity);
    }

    /**
     * 保存拖拽数据(父节点和路径)
     */
    @ResponseBody
    @PostMapping("/dnd")
    @Authorize
    public RestMessage dnd(@RequestBody List<TreeNodeIndex> nodeIndexs) {
        return dicCategoryService.batchUpdate(TreeHelper.toPrimaryKeyValues(nodeIndexs));
    }

    /**
     * 删除字典类型
     */
    @ResponseBody
    @PostMapping("/delete")
    @Authorize
    public RestMessage delete(String id) {
        return dicCategoryService.delete(id);
    }

    /**
     * 导出字典类型
     */
    @GetMapping("/export")
    @Authorize
    public void export() {
        ExcelHelper.exportWeb(dicCategoryService.selectList(), SysDicCategory.class, "系统字典类型");
    }

    //endregion
}
